var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    wrap_one:true,
    ifTrue: false,
    Length: 6,        //输入框个数
    isFocusOne: true,    //聚焦
    ValueOne: "",        //输入的内容
    // isFocusTwo: true,    //聚焦
    ValueTwo: "",        //输入的内容
    ispassword: true //是否密文显示 true为密文， false为明文。
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  onFocusOne: function(e){
    var that = this;
    console.log(e.detail.value);
    var inputValue = e.detail.value.trim();
    that.setData({
      ValueOne: inputValue,
    })
    if (inputValue.length >= 6){
      that.setData({ wrap_one: false});
    }
  },

  onTapOne: function(e) {
    console.log(e.currentTarget.dataset.index);
    var that = this;
    that.setData({
      isFocusOne: true,
    })
  },
  onFocusTwo: function (e) {
    var that = this;
    console.log(e.detail.value);
    var inputValue = e.detail.value.trim();
    that.setData({
      ValueTwo: inputValue,
    })
  },

  onTapTwo: function (e) {
    console.log(e.currentTarget.dataset.index);
    var that = this;
    that.setData({
      isFocusTwo: true,
    })
  },
  
  formSubmit: function(e) {
    var that = this;
    if (that.data.ValueOne == "" || that.data.ValueTwo==""){
      wx.showToast({
        title: '各项信息必填',
        icon: "none"
      })
      return false;
    } else if (that.data.ValueOne.length != 6 || that.data.ValueTwo.length!=6){
      wx.showToast({
        title: '密码长度有误',
        icon: "none"
      })
      return false;
    } else if (that.data.ValueOne != that.data.ValueTwo){
      wx.showToast({
        title: '2次输入不一致',
        icon: "none"
      })
      return false;
    }
    if (that.data.ValueOne == that.data.ValueOne && that.data.ValueOne.length == that.data.ValueTwo.length){
      that.setData({ ifTrue:true});
        wx.request({
          method: "POST",
          url: 'https://www.zaishengwang.com/wx/setPay.do',
          dataType: 'json',
          header: {
            'content-type': 'application/x-www-form-urlencoded',
          },
          data: {
            userId: app.globalData.userId,
            password: that.data.ValueOne,
          },
          success: function (res) {
            var c = 3;
            if(res.data.msg=="1"){
              wx.showToast({
                title: '密码设置成功',
                icon: 'success',
                duration: 1500
              });
              var intervalId = setInterval(function () {
                c = c - 1;
                if (c == 0) {
                  clearInterval(intervalId);
                  wx.navigateBack({
                    delta: 2
                  })
                }
              }, 1000)
             
            }else{
              that.setData({ ifTrue: false });
            }

          },
          fail: function (error) {
            console.log(error);
          }


    }) 
    // }else{
    //   wx.showModal({
    //     title: '2次密码必填且值相同长度为6',
    //     confirmColor: '#56BDE0',
    //     showCancel: false,
    //   })
     }
  },


  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})